package net.xiangcaowuyu;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.Vector;

public class Main {

    public static void main(String[] args) {
        User user = new User(1, "张三");
        WeakReference<User> userSoftReference = new WeakReference<>(user);
        user=null;
        System.out.println(userSoftReference.get());
        System.gc();
        System.out.println("After GC:");
        System.out.println(userSoftReference.get());
        Vector<byte[]> vector = new Vector<>();
        for (int i = 0; i < 19; i++) {
            vector.add(new byte[1 * 950 * 1024]);
        }
        System.gc();
        System.out.println("After GC:");
        System.out.println(userSoftReference.get());
    }

    static class User {
        private int id;
        private String name;

        public User(int id, String name) {
            this.id = id;
            this.name = name;
        }

        @Override
        public String toString() {
            return "id=" + id + ";name=" + name;
        }
    }

    public static void printRuntime() {
        System.out.println("maxMemory:" + Runtime.getRuntime().maxMemory() / 1024 / 1024);
        System.out.println("totalMemory:" + Runtime.getRuntime().totalMemory() / 1024 / 1024);
        System.out.println("freeMemory:" + Runtime.getRuntime().freeMemory() / 1024 / 1024);
    }
}
